package com.ruoyi.trash.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.trash.mapper.TrashMapper;
import com.ruoyi.trash.domain.Trash;
import com.ruoyi.trash.service.ITrashService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 回收站Service业务层处理
 *
 * @author huacai
 * @date 2024-07-10
 */
@Service
public class TrashServiceImpl implements ITrashService
{
    @Autowired
    private TrashMapper trashMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询回收站
     *
     * @param trashId 回收站主键
     * @return 回收站
     */
    @Override
    public Trash selectTrashByTrashId(Integer trashId)
    {
        return trashMapper.selectTrashByTrashId(trashId);
    }

    /**
     * 查询回收站列表
     *
     * @param trash 回收站
     * @return 回收站
     */
    @Override
    public List<Trash> selectTrashList(Trash trash)
    {
        return trashMapper.selectTrashList(trash);
    }

    /**
     * 新增回收站
     *
     * @param trash 回收站
     * @return 结果
     */
    @Override
    public int insertTrash(Trash trash)
    {
        trash.setCreateTime(DateUtils.getNowDate());
        return trashMapper.insertTrash(trash);
    }

    /**
     * 批量新增回收站
     *
     * @param trashs 回收站List
     * @return 结果
     */
    @Override
    public int batchInsertTrash(List<Trash> trashs)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(trashs)) {
            try {
                for (int i = 0; i < trashs.size(); i++) {
                    int row = trashMapper.insertTrash(trashs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == trashs.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改回收站
     *
     * @param trash 回收站
     * @return 结果
     */
    @Override
    public int updateTrash(Trash trash)
    {
        trash.setUpdateTime(DateUtils.getNowDate());
        return trashMapper.updateTrash(trash);
    }

    /**
     * 批量删除回收站
     *
     * @param trashIds 需要删除的回收站主键
     * @return 结果
     */
    @Override
    public int deleteTrashByTrashIds(Integer[] trashIds)
    {
        return trashMapper.deleteTrashByTrashIds(trashIds);
    }

    /**
     * 删除回收站信息
     *
     * @param trashId 回收站主键
     * @return 结果
     */
    @Override
    public int deleteTrashByTrashId(Integer trashId)
    {
        return trashMapper.deleteTrashByTrashId(trashId);
    }
}
